/**
 * Copyright 2010 Creative Works, Inc.
 * Creative Works licenses this file to you under the Apache License, version
 * 2.0 (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *    http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package org.sendvox.freswitch.outbound;

import java.util.List;
import java.util.Map;

import org.jboss.netty.channel.Channel;
import org.sendvox.freswitch.core.ApiCommand;
import org.sendvox.freswitch.core.CommandException;
import org.sendvox.freswitch.core.NetAddress;
import org.sendvox.freswitch.core.SendMsg;
import org.sendvox.freswitch.core.SleepCommand;
import org.sendvox.freswitch.core.SpeakCommand;
import org.sendvox.freswitch.core.impl.ApiCommandImpl;
import org.sendvox.freswitch.core.impl.SendMsgImpl;
import org.sendvox.freswitch.core.impl.SleepCommandImpl;
import org.sendvox.freswitch.core.impl.SpeakCommandImpl;
import org.sendvox.freswitch.core.impl.SyncService;
import org.sendvox.freswitch.event.impl.EventSocketMessageListener;
import org.sendvox.freswitch.event.impl.Message;
import org.sendvox.freswitch.event.impl.RawMessage;

public abstract class OutboundClient implements SyncService {

	public ApiCommand api = new ApiCommandImpl(getThis());

	public SendMsg sendmsg = new SendMsgImpl(getThis());

	public SleepCommand sleep = new SleepCommandImpl(getThis());

	public SpeakCommand speak = new SpeakCommandImpl(getThis());

	public abstract void connect(NetAddress netAddress)
			throws OutboundException;

	public abstract void configurationOption(Map<Option, String> option);

	public abstract void disconnect() throws OutboundException;

	public abstract OutboundClient getThis();

	public abstract void addMessageHandler(
			EventSocketMessageListener<Message> observer);

	public abstract void deleteEventSocketMessageListener(
			EventSocketMessageListener<Message> observer);

	public abstract RawMessage executeSyncMultiLineCommand(Channel channel,
			List<String> command);

	public abstract RawMessage execute(String fullCommand)
			throws CommandException;

	public enum Option {
		CHILD_TCPNODELAY("child.tcpNoDelay"), CHILD_KEEPALIVE("child.keepAlive"), TIMEOUT_SECONDS(
				"connectTimeoutSeconds"), NUMB_CONNECTIONS("numbConnections");

		private final String literal;

		private Option(String literal) {
			this.literal = literal;
		}

		public String getLiteral() {
			return literal;
		}

		public static Option fromLiteral(String literal) {
			for (Option name : values()) {
				if (name.literal.equalsIgnoreCase(literal)) {
					return name;
				}
			}
			return null;
		}

	}

}
